package com.deepglint.state;

import com.deepglint.beans.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.runtime.state.ArrayListSerializer;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.checkpoint.ListCheckpointed;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.util.Collections;
import java.util.List;

/**
 * @author mj
 * @version 1.0
 * @date 2021-11-21 22:42
 */
public class StateTest_OperatorState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
//        env.setStreamTimeCharacteristic(); // 1.12开始，默认的就是eventTime
        env.getConfig().setAutoWatermarkInterval(200);// 设置周期性的waterMark的周期时间

        DataStream<String> streamSource = env.socketTextStream("192.168.150.128", 7777);

        DataStream<SensorReading> dataStream = streamSource.map(line -> {
            String[] split = line.split(",");
            return new SensorReading(split[0], split[1], new Long(split[2]), new Double(split[3]));
        });

        SingleOutputStreamOperator<Integer> mapStream = dataStream.map(new MyCountMapper());

        mapStream.print();

        env.execute();
    }

    static class MyCountMapper implements MapFunction<SensorReading, Integer>, ListCheckpointed<Integer> {

        // 定义一个本地变量，作为算子状态
        private Integer count = 0;

        @Override
        public Integer map(SensorReading value) throws Exception {
            return ++count;
        }

        @Override
        public List<Integer> snapshotState(long checkpointId, long timestamp) throws Exception {
            return Collections.singletonList(count);
        }

        @Override
        public void restoreState(List<Integer> state) throws Exception {
            for (Integer num : state) {
                count += num;
            }
        }
    }

    // 故障状态恢复，新版
//    static class MyCountMapper implements MapFunction<SensorReading, Integer>, CheckpointedFunction {
//
//        // 定义一个本地变量，作为算子状态
//        private Integer count = 0;
//
//        @Override
//        public Integer map(SensorReading value) throws Exception {
//            return ++count;
//        }
//
//        @Override
//        public void snapshotState(FunctionSnapshotContext context) throws Exception {
//            long checkpointId = context.getCheckpointId();
//            long checkpointTimestamp = context.getCheckpointTimestamp();
//        }
//
//        @Override
//        public void initializeState(FunctionInitializationContext context) throws Exception {
//        }
//    }
}
